	package com.vnp.cooking.adapter;

import java.util.ArrayList;

import com.vnp.cooking.R;
import com.vnp.cooking.R.id;
import com.vnp.cooking.R.layout;
import com.vnp.cooking.bean.ObjNguyenlieu;
import com.vnp.cooking.utils.Utils;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListNguyenlieuAdapter extends ArrayAdapter<ObjNguyenlieu>{
	Context context;
	ArrayList<ObjNguyenlieu> objNguyenlieus = new ArrayList<ObjNguyenlieu>();
	
	public ListNguyenlieuAdapter(Context context, ArrayList<ObjNguyenlieu> objNguyenlieus) {
		super(context, R.layout.list_nguyen_lieu_item, objNguyenlieus);
		this.context = context;
		this.objNguyenlieus = objNguyenlieus;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	
	
	@Override
	public int getCount() {
		return objNguyenlieus.size();
	}


	@Override
	public View getView(int position, View v, ViewGroup parent) {
		ViewHolder holder = null;
		
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.list_nguyen_lieu_item, parent, false);
			
			holder = new ViewHolder();
			
			holder.txtNguyenlieu = (TextView) v.findViewById(R.id.txt_nguyenlieu);
			holder.txtSoluong = (TextView) v.findViewById(R.id.txt_soluong);
			
			holder.txtNguyenlieu.setTypeface(Utils.getTypefaceRegular(context), Typeface.BOLD);
			holder.txtSoluong.setTypeface(Utils.getTypefaceRegular(context), Typeface.BOLD);
			v.setTag(holder);
			
		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		// set gia tri
		/*if (objNguyenlieus.get(position).getNguyenLieu().length() > 15) {
			holder.txtNguyenlieu.setText(objNguyenlieus.get(position).getNguyenLieu().substring(0, 15)+"...");
		} else {*/
			holder.txtNguyenlieu.setText(objNguyenlieus.get(position).getNguyenLieu());
//		}
		
		if (objNguyenlieus.get(position).getSoLuong().length() > 15){
			holder.txtSoluong.setText(objNguyenlieus.get(position).getSoLuong().substring(0, 15)+"...");
		} else {
			holder.txtSoluong.setText(objNguyenlieus.get(position).getSoLuong());
		}
		
		
		return v;
	}
	
	
	// optimize listview
	class ViewHolder {
		boolean checked;
		TextView txtNguyenlieu;
		TextView txtSoluong;
	}
	

}
